import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
// } from './grouter/index'
import Layout from '../layout/index.vue'
import Home from '../pages/home.vue'
import About from '../pages/about.vue'
import Login from '../components/Login.vue'
const NoPage = () => import('../components/404.vue')
import {getToken} from "../utils/auth"

let routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    name: 'path-app',
    meta: {
      hidden: false
    },
    children: [
      {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
          hidden: false
        }
      },
      {
        path: '/about',
        name: 'About',
        component: About,
        meta: {
          hidden: false
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      hidden: true
    }
  },
  {
    path: '/404',
    name: 'NoPage404',
    component: NoPage,
    meta: {
      hidden: true
    }
  }
]
// debugger
// let temp = JSON.parse(localStorage.getItem('routers')),routes=[]
// if (temp.value && temp.value.length) {
//   routes = temp
// }

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach(async (to, from, next) => {
  let token = getToken()
  if (to.path === '/login')  next()
  if (!token) {
    console.log(111, token)
    next({ path: '/login' })
  } else {
    next()
  }
})

export default router
